package com.fbinggun.fbinggunutil.statistics;

import com.alibaba.fastjson.JSONObject;
import com.fbinggun.fbinggunutil.API.TiemUtil;
import com.fbinggun.fbinggunutil.FBinggunUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class PlayerStatistics {
    Map<String,StatisticsInfo> data = new HashMap<>();

    String playerName;

    StatistMain main;
    protected PlayerStatistics(String playerName) {
        this.playerName = playerName;
        main=FBinggunUtil.plugin.getStatistMain();
    }

    protected PlayerStatistics(JSONObject jsonObject) {
        main=FBinggunUtil.plugin.getStatistMain();
        this.playerName = jsonObject.getString("Player");
        JSONObject jsonData = jsonObject.getJSONObject("Info");
        if(jsonData!=null){
            for(String id: jsonData.keySet()){
                JSONObject infoData = jsonData.getJSONObject(id);
               StatisticsInfo info= StatisticsInfo.getInstance(id,infoData);
               if(info!=null){
                   data.put(id,info);
               }else {
                   System.out.println("玩家"+playerName+" 统计数据错误 "+id+" 统计不存在");
               }
            }
        }
    }
    protected JSONObject toJson(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Player",playerName);
        JSONObject dataJson= new JSONObject();
        for(StatisticsInfo statisticsInfo:this.data.values()){
            dataJson.put(statisticsInfo.id,statisticsInfo.toJson());
        }
        if(!dataJson.isEmpty()){
            jsonObject.put("Info",dataJson);
            return jsonObject;
        }
        return null;
    }

    public Map<String, StatisticsInfo> getData() {
        return data;
    }

    public void delStatisticsInfo(String id){
        data.remove(id);
        save();
    }
    public void delAll(){
        data.clear();
        save();
    }

    protected StatisticsInfo getStatisticsInfo(String id){
        return data.get(id);
    }
    protected String getInfoV(String id){
        StatisticsInfo statisticsInfo=getStatisticsInfo(id);
        if(statisticsInfo==null){
            return "";
        }
        return statisticsInfo.getPlaceholder();
    }

    /**
     * 增加统计值
     * @param id
     * @param t
     * @param <T>
     */
    protected <T> void addStatisticsInfo(String id,T t){
        StatisticsInfo<T> statisticsInfo=getStatisticsInfo(id);
        if(statisticsInfo==null){
           setStatisticsInfo(id,t);
        }else {
            statisticsInfo.addValue(t);
        }
        save();
    }
    /**
     * 增加统计值
     * @param id
     * @param t
     * @param
     */
    protected  void addSStatisticsInfo(String id,String t){
        StatisticsInfo statisticsInfo=getStatisticsInfo(id);
        if(statisticsInfo==null){
            setSStatisticsInfo(id,t);
        }else {
            statisticsInfo.addSValue(t);
        }
        save();
    }

    /**
     * 设置统计信息
     * @param id
     * @param t
     * @param <T>
     */
    protected <T> void setStatisticsInfo(String id,T t){
        StatisticsInfo<T> statisticsInfo=getStatisticsInfo(id);
        if(statisticsInfo==null){
            statisticsInfo= main.getProject().getStatisticsInfo(id);
            if(statisticsInfo==null){
                System.out.println("玩家"+playerName+" 统计数据错误 "+id+" 统计不存在");
                return;
            }
            statisticsInfo.setValue(t);
            statisticsInfo.setClockTime(TiemUtil.date());
            statisticsInfo.setFirstClock(TiemUtil.date());
            data.put(id, statisticsInfo);

        }else {
            statisticsInfo.setValue(t);
        }
        save();
    }



    protected <T> void setSStatisticsInfo(String id,String t){
        StatisticsInfo<T> statisticsInfo=getStatisticsInfo(id);
        if(statisticsInfo==null){
            statisticsInfo= main.getProject().getStatisticsInfo(id);
            if(statisticsInfo==null){
                System.out.println("玩家"+playerName+" 统计数据错误 "+id+" 统计不存在");
                return;
            }
            statisticsInfo.setSValue(t);
            statisticsInfo.setClockTime(TiemUtil.date());
            statisticsInfo.setFirstClock(TiemUtil.date());
            data.put(id, statisticsInfo);

        }else {
            statisticsInfo.setSValue(t);
        }
        save();
    }



    protected void save(){
        JSONObject json = toJson();
        if(json==null){
            try {
                main.getStatisticsTable().delJson(playerName);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }else {
            try {
                main.getStatisticsTable().setJson(playerName,toJson());
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

    protected Long getClockTime(String id) {
        StatisticsInfo statisticsInfo=getStatisticsInfo(id);
        if(statisticsInfo==null){
            return 0l;
        }
       return statisticsInfo.getClockTime();
    }
    protected Long getFirstClock(String id) {
        StatisticsInfo statisticsInfo=getStatisticsInfo(id);
        if(statisticsInfo==null){
            return 0l;
        }
        return statisticsInfo.getFirstClock();
    }





    protected void setClockTime(String id,Long clockTime) {
        StatisticsInfo statisticsInfo=getStatisticsInfo(id);
        if(statisticsInfo==null){
            return;
        }
        statisticsInfo.setClockTime(clockTime);
    }


}
